package com.onlyxiahui.extend.query.hibernate.handler.result.config;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.beans.propertyeditors.CustomDateEditor;

/**
 * Description <br>
 * Date 2019-05-21 15:16:53<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class AllCustomDateEditor extends CustomDateEditor {

	public AllCustomDateEditor() {
		super(new SimpleDateFormat(DateTimeUtil.FORMAT_DATE_TIME_MILLISECOND), true);
	}

	public AllCustomDateEditor(DateFormat dateFormat, boolean allowEmpty) {
		super(dateFormat, allowEmpty);
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (null != text) {
			try {
				text = DateTimeUtil.allToDateTimeMillisecond(text);
				SimpleDateFormat dateFormat = new SimpleDateFormat(DateTimeUtil.FORMAT_DATE_TIME_MILLISECOND);
				setValue(dateFormat.parse(text));
			} catch (ParseException ex) {
				throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
			}
		} else {
			setValue(null);
		}
	}
}
